package com.department.mydepartment.controller;

import com.department.mydepartment.service.ArticleService;
import com.department.mydepartment.util.ShowData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    ShowData showData;

    //创建文章(带封面)
    @PostMapping(path = "/createWithCover")
    public String createWithCover(@RequestParam("articleTitle") String articleTitle,
                                  @RequestParam("articleTag") String articleTag,
                                  @RequestParam("articleType") String articleType,
                                  @RequestParam("content") String content,
                                  @RequestParam("img") MultipartFile img) {
        if ("".equals(articleTitle) || articleTitle == null || "".equals(articleTag) || articleTag == null
                || "".equals(articleType) || articleType == null) {
            return showData.showData(false, "请补全信息");
        } else if (img == null) {
            return showData.showData(false, "封面图片不能为空");
        } else if ("".equals(content) || content == null) {
            return showData.showData(false, "文章内容不能为空");
    }
        return articleService.createWithCover(articleTitle, articleTag, articleType, content, img);
    }

    //创建文章(不带封面)
    @PostMapping(path = "/createWithoutCover")
    public String createWithoutCover(@RequestParam("articleTitle") String articleTitle,
                                     @RequestParam("articleTag") String articleTag,
                                     @RequestParam("articleType") String articleType,
                                     @RequestParam("content") String content){
        if ("".equals(articleTitle) || articleTitle == null || "".equals(articleTag) || articleTag == null
                || "".equals(articleType) || articleType == null) {
            return showData.showData(false, "请补全信息");
        } else if ("".equals(content) || content == null) {
            return showData.showData(false, "文章内容不能为空");
        }
        return articleService.createWithoutCover(articleTitle, articleTag, articleType, content);
    }

    //查询
    @GetMapping(path = "/findAllArticle")
    public String findAllArticle() {
        return articleService.findAllArticle();
    }

    @GetMapping(path = "/findByArticleId")
    public String findByArticleId(@RequestParam("articleId") String articleId) {
        if ("".equals(articleId) || articleId == null) {
            return showData.showData(false, "文章不存在");
        }
        return articleService.findByArticleId(articleId);
    }

    @GetMapping(path = "/findByArticleTitle")
    public String findByArticleTitle(@RequestParam("articleTitle") String articleTitle) {
        if ("".equals(articleTitle) || articleTitle == null) {
            return showData.showData(false, "请输入文章标题");
        }
        return articleService.findByArticleTitle(articleTitle);
    }
    @GetMapping(path = "/findByArticleTag")
    public String findByArticleTag(@RequestParam("articleTag") String articleTag) {
        if ("".equals(articleTag) || articleTag == null) {
            return showData.showData(false, "文章不存在");
        }
        return articleService.findByArticleTag(articleTag);
    }
    @GetMapping(path = "/findByArticleType")
    public String findByArticleType(@RequestParam("articleType") String articleType) {
        if ("".equals(articleType) || articleType == null) {
            return showData.showData(false, "文章不存在");
        }
        return articleService.findByArticleType(articleType);
    }

    //编辑
    @PostMapping(path = "/editByArticleId")
    public String editByArticleId(@RequestParam("articleId") String articleId,
                                  @RequestParam("articleTitle") String articleTitle,
                                  @RequestParam("content") String content
                                  ) {
        if ("".equals(articleId) || articleId == null || "".equals(articleTitle) || articleTitle == null ||
                "".equals(content) || content == null) {
            return showData.showData(false, "请完善信息");
        }
        return articleService.editByArticleId(articleId, articleTitle, content);
    }

    //删除
    @DeleteMapping(path = "/deleteByArticleId")
    public String deleteByArticleId(@RequestParam("articleId") String articleId) {
        if ("".equals(articleId) || articleId == null ) {
            return showData.showData(false, "文章不存在");
        }
        return articleService.deleteByArticleId(articleId);
    }

    @PostMapping(path = "/deleteArticleList")
    public String deleteArticleList(@RequestParam("articleIdList") String[] articleIdList){
        if (articleIdList.length == 0) {
            return showData.showData(false, "请选择文章");
        }
        return articleService.deleteArticleList(articleIdList);
    }

    //获取文章内容
    @GetMapping("/getArticleContent")
    public String getArticleContent(@RequestParam("articleId") String articleId) throws IOException {
        if ("".equals(articleId) || articleId == null ) {
            return showData.showData(false, "文章不存在");
        }
        return articleService.getArticleContent(articleId);
    }
}
